﻿Imports CapaLogica
Imports CapaLogica.CL.Carrera
Imports CapaLogica.CL.Usuario

Public Class frmCargarMasivaEstudiantes

    Private Sub tsbCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbCancelar.Click
        Me.Dispose()
    End Sub

    Private Sub btn__Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_.Click
        fileExport.ShowDialog()
        txtPath.Text = fileExport.FileName
    End Sub


    Private Sub tsbCargarArchivo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbCargarArchivo.Click

        Try
            If txtPath.Text.Length Then
                Dim MyConnection As System.Data.OleDb.OleDbConnection
                Dim DtSet As System.Data.DataSet
                Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
                Dim source As String = txtPath.Text

                MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + source + "';Extended Properties=Excel 8.0;")
                MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [Hoja1$]", MyConnection)
                MyCommand.TableMappings.Add("Table", "Net-informations.com")
                DtSet = New System.Data.DataSet
                MyCommand.Fill(DtSet)
                dataEstudiantes.DataSource = DtSet.Tables(0)
                MyConnection.Close()

                tsbGuardarDatos.Enabled = True
            Else
                MsgBox("Debe de seleccionar un archivo")
            End If

        Catch ex As Exception
            MsgBox("Ocurrio un error al leer el archivo")
        End Try
      
    End Sub

    Private Sub frmCargarMasivaEstudiantes_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Refresh()
    End Sub

    Private Sub tsbGuardarDatos_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbGuardarDatos.Click
        Try
            Dim i As Integer = 0
            Dim j As Integer = 0
            Dim rows As Integer = 0
            Dim columns As Integer = 0
            Dim listaM As List(Of List(Of String)) = New List(Of List(Of String))
            Dim listInterna As List(Of String)


            rows = dataEstudiantes.RowCount - 2
            columns = dataEstudiantes.ColumnCount - 1

            ''Recorre el datagrid
            For i = 0 To rows
                listInterna = New List(Of String)
                For j = 0 To columns
                    listInterna.Add(CStr(dataEstudiantes.Item(j, i).Value))
                Next j
                listaM.Add(listInterna)
            Next i

            If GestorUsuario.registrarUsuarioBatch(listaM) Then
                MessageBox.Show("Se cargo el archivo correctamente", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                MessageBox.Show("Ocurrio un error en la carga", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try


    End Sub
End Class